﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AugustSql
{
    public class DateTimeHelper
    {

        private static int dayStartHourStatic;

        private static int dayStartMinuteStatic;

        private static int dayEndHourStatic;

        private static int dayEndMinuteStatic;

        private static int nightStartHourStatic;

        private static int nightStartMinuteStatic;

        private static bool isInitialized = false;

        public static string[] GetDataShifDetail(int dayStartHour, int dayStartMinute, int dayEndHour, int dayEndMinute, int nightStartHour, int nightStartMinute)
        {
            return GetDataShifDetail(DateTime.Now, dayStartHour, dayStartMinute, dayEndHour, dayEndMinute, nightStartHour, nightStartMinute);
        }

        public static string[] GetDataShifDetail()
        {
            if (!isInitialized) throw new Exception("未设置班次起末时间");
            return GetDataShifDetail(DateTime.Now, dayStartHourStatic, dayStartMinuteStatic, dayEndHourStatic, dayEndMinuteStatic, nightStartHourStatic, nightStartMinuteStatic);
        }


        /// <summary>
        /// 获取前一班次信息
        /// </summary>
        /// <param name="dayStartHour"></param>
        /// <param name="dayStartMinute"></param>
        /// <param name="dayEndHour"></param>
        /// <param name="dayEndMinute"></param>
        /// <param name="nightStartHour"></param>
        /// <param name="nightStartMinute"></param>
        /// <returns></returns>
        public static string[] GetForeDataShifDetail(int dayStartHour, int dayStartMinute, int dayEndHour, int dayEndMinute, int nightStartHour, int nightStartMinute)
        {
            return GetDataShifDetail(DateTime.Now.AddHours(-12), dayStartHour, dayStartMinute, dayEndHour, dayEndMinute, nightStartHour, nightStartMinute);
        }

        public static string[] GetForeDataShifDetail()
        {
            if (!isInitialized) throw new Exception("未设置班次起末时间");
            return GetDataShifDetail(DateTime.Now.AddHours(-12), dayStartHourStatic, dayStartMinuteStatic, dayEndHourStatic, dayEndMinuteStatic, nightStartHourStatic, nightStartMinuteStatic);
        }

        public static string[] GetDataShifDetail(DateTime dateTimeNow, int dayStartHour, int dayStartMinute, int dayEndHour, int dayEndMinute, int nightStartHour, int nightStartMinute)
        {

            dayStartHourStatic = dayStartHour;
            dayStartMinuteStatic = dayStartMinute;
            dayEndHourStatic = dayEndHour;
            dayEndMinuteStatic = dayEndMinute;
            nightStartHourStatic = nightStartHour;
            nightStartMinuteStatic = nightStartMinute;
            isInitialized = true;
            string[] retStr = new string[4];
            TimeSpan timeSpan1 = dateTimeNow - new DateTime(dateTimeNow.Year, dateTimeNow.Month, dateTimeNow.Day, dayStartHour, dayStartMinute, 0);
            TimeSpan timeSpan2 = dateTimeNow - new DateTime(dateTimeNow.Year, dateTimeNow.Month, dateTimeNow.Day, dayEndHour, dayEndMinute, 0);
            TimeSpan timeSpan = new DateTime(2000, 1, 1, nightStartHour, nightStartMinute, 0) - new DateTime(2000, 1, 1, dayEndHour, dayEndMinute, 0);
            if (timeSpan1.Ticks > 0 - timeSpan.Ticks / 2 && timeSpan2.Ticks < timeSpan.Ticks / 2)
            {
                retStr[0] = "白班";
            }
            else
            {
                retStr[0] = "夜班";
            }

            if (dateTimeNow > new DateTime(dateTimeNow.Year, dateTimeNow.Month, dateTimeNow.Day, dayStartHour, dayStartMinute, 0).AddTicks(-timeSpan.Ticks / 2))
            {
                retStr[1] = dateTimeNow.ToString("yyyy-MM-dd");
            }
            else
            {
                retStr[1] = dateTimeNow.AddDays(-1).ToString("yyyy-MM-dd");
            }

            if (retStr[0] == "白班")
            {
                retStr[2] = new DateTime(dateTimeNow.Year, dateTimeNow.Month, dateTimeNow.Day, dayStartHour, dayStartMinute, 0).AddTicks(-timeSpan.Ticks / 2).ToString("yyyy-MM-dd HH:mm:ss");
                retStr[3] = new DateTime(dateTimeNow.Year, dateTimeNow.Month, dateTimeNow.Day, dayEndHour, dayEndMinute, 0).AddTicks(timeSpan.Ticks / 2).ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                DateTime dateTime = DateTime.Parse(retStr[1]);
                DateTime dateTime2 = dateTime.AddDays(1);

                retStr[2] = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, nightStartHour, nightStartMinute, 0).AddTicks(-timeSpan.Ticks / 2).ToString("yyyy-MM-dd HH:mm:ss");
                retStr[3] = new DateTime(dateTime2.Year, dateTime2.Month, dateTime2.Day, dayStartHour, dayStartMinute, 0).AddTicks(-timeSpan.Ticks / 2).ToString("yyyy-MM-dd HH:mm:ss");
            }

            return retStr;
        }
    }
}
